package com.lz.football.module.main.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.TipsConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.message.http.JsClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.ClubInformation;
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.item.IconItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.LLabel;
	import lz.controls.managers.TipsManager;

	/**
	 *用户信息面板 
	 * @author lxh
	 * 
	 */	
	public class UserInfoPanel extends Sprite implements IComponent
	{
		/**
		 *皮肤 
		 */		
		public var symbolSkin			:MovieClip;
		/**
		 *用户名 
		 */		
		private var txtUserName 		:TextField;
		/**
		 *用户名label 
		 */		
		private var userNameLabel		:LLabel;
		/**
		 *等级 
		 */		
		private var txtLevel			:TextField;
		/**
		 *vip 
		 */		
		private var txtVip				:TextField;
		/**
		 *点券 
		 */		
		private var txtCashpoint 		:TextField;
		/**
		 *金币 
		 */		
		private var txtCoin	 		:TextField;
		/**
		 *游戏币 
		 */		
		private var txtGamegold		:TextField;
		/**
		 *俱乐部队徽容器 
		 */		
		private var clubIconContainer	:MovieClip;
		/**
		 *经验进度 
		 */		
		private var expMC				:MovieClip;
		/**
		 *金币增加按钮 
		 */		
		private var gamegoldAddBtn		:SimpleButton;
		/**
		 *点券增加按钮 
		 */		
		private var cashAddBtn			:SimpleButton;
		/**
		 *欧元增加按钮 
		 */		
		private var euroAddBtn			:SimpleButton;
		/**
		 *buff面板 
		 */		
		private var buffListPanel		:BuffListPanel;
		
		public function UserInfoPanel()
		{
			super();
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			symbolSkin 		= view as MovieClip;
			txtUserName 	= symbolSkin.mc1.txtUserName;
			/*userNameLabel = new LLabel();
			userNameLabel.x = txtUserName.x;
			userNameLabel.y = txtUserName.y;
			userNameLabel.textField = txtUserName;
			userNameLabel.width = txtUserName.width;
			userNameLabel.height = txtUserName.height
			symbolSkin.mc1.addChild(userNameLabel);*/
			txtLevel 		= symbolSkin.txtLevel;
			txtVip			= symbolSkin.txtVip;
			expMC			= symbolSkin.expMC;
			txtCashpoint 	= symbolSkin.mc2.txtCashpoint;
			cashAddBtn		= symbolSkin.cashAddBtn;
			euroAddBtn		= symbolSkin.euroAddBtn;
			txtCoin 		= symbolSkin.mc3.txtCoin;
			txtGamegold 	= symbolSkin.mc4.txtGamegold;
			clubIconContainer = symbolSkin.clubIconContainer;
			txtUserName.text 	= "";
			txtLevel.text 		= "";
			txtCashpoint.text 	= "";
			txtCoin.text 		= "";
			txtGamegold.text 	= "";
			txtUserName.mouseEnabled = false;
			txtLevel.mouseEnabled = false;
			addEvtListener();
			//icon
			/*var iconItem :IconItem = new IconItem();
			iconItem.data = "com.lz.res.Coin3";
			iconItem.tips = "点券";
			symbolSkin.mc2.container.addChild(iconItem);
			iconItem = new IconItem();
			iconItem.data = "com.lz.res.Coin1";
			iconItem.tips = "欧元";
			symbolSkin.mc3.container.addChild(iconItem);
			iconItem = new IconItem();
			iconItem.data = "com.lz.res.Coin2";
			iconItem.tips = "金币";
			symbolSkin.mc4.container.addChild(iconItem);*/
			buffListPanel = new BuffListPanel();
			symbolSkin.addChild(buffListPanel);
			buffListPanel.y = 114;
			buffListPanel.x = 25;
			TweenLite.delayedCall(2,function():void{buffListPanel.initUI()});
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.mc3,"欧元");
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.mc2,"点券");
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.mc4,"声望");
			DisplayMouseEventManager.instance.addTipsDisplay(cashAddBtn,"点券充值");
			DisplayMouseEventManager.instance.addTipsDisplay(euroAddBtn,"购买欧元");
		}
		
		override public function get visible():Boolean
		{
			return super.visible;
		}
		
		override public function set visible(value:Boolean):void
		{
			super.visible = value;
			symbolSkin.visible = value;
		}
		/**
		 * 
		 * 设置数据
		 */		
		public function setData():void
		{
			var clubInfo :ClubInformation = AppData.getInstance().clubDatamanager.clubInfo;
			txtUserName.text 	= "" + clubInfo.uname;
			//userNameLabel.text = "" + clubInfo.uname;
			txtLevel.text 		= "" + clubInfo.level;
			//txtCashpoint.text 	= "" + clubInfo.cashpoint;
			cashPoint			= clubInfo.cashpoint;
			//txtCoin.text 		= "" + clubInfo.coin;
			coin				= clubInfo.coin;
			//txtGamegold.text	= "" + clubInfo.fame;
			gameGold 			= clubInfo.fame;
			txtVip.text			= "" + clubInfo.VIP;
			setTeamIcon(clubInfo.logo);
			//exp
			var curExp :int = clubInfo.exp;
			var nxtExp :int = clubInfo.nextexp;
			var curframe :int = expMC.totalFrames * curExp / nxtExp;
			expMC.gotoAndStop(curframe);
		}
		/**
		 *设置队徽 
		 * 
		 */
		private function setTeamIcon($iconId:int):void
		{
			//自己队徽
			var iconId :int = AppData.getInstance().clubDatamanager.clubInfo.logo;
			var iconDisp :DisplayObject = Reflection.createInstance("com.football.team.icon_"+iconId);
			if(iconDisp == null)return;
			var showSize :Number = 60;
			var scxy :Number = ((showSize / iconDisp.width) <= (showSize / iconDisp.height))?(showSize / iconDisp.width):(showSize / iconDisp.height);
			iconDisp.scaleX = scxy;
			iconDisp.scaleY = scxy;
			DisplayObjectUtil.removeAllChildren(clubIconContainer);
			clubIconContainer.addChild(iconDisp);
			iconDisp.x = (showSize - iconDisp.width)/2;
			iconDisp.y = (showSize - iconDisp.height)/2;
		}
		/**
		 * 设置金币
		 * @param value
		 * 
		 */		
		public function set coin(value :int):void
		{
			var lastValue :int = int(txtCoin.text);
			var curValue :int = value;
			showCoinEffects(lastValue,curValue,1,symbolSkin.mc3);
			//txtCoin.text = value + "";
			changeEff(lastValue,curValue,txtCoin);
		}
		/**
		 * 设置点券
		 * @param value
		 * 
		 */		
		public function set cashPoint(value :int):void
		{
			var lastValue :int = int(txtCashpoint.text);
			var curValue :int = value;
			showCoinEffects(lastValue,curValue,3,symbolSkin.mc2);
			//txtCashpoint.text = value + "";
			changeEff(lastValue,curValue,txtCashpoint);
		}
		/**
		 * 设置游戏币
		 * @param value
		 * 
		 */		
		public function set gameGold(value :int):void
		{
			var lastValue :int = int(txtGamegold.text);
			var curValue :int = value;
			showCoinEffects(lastValue,curValue,2,symbolSkin.mc4);
			//txtGamegold.text = value + "";
			changeEff(lastValue,curValue,txtGamegold);
		}
		/**
		 * 钱币数字变化 
		 * @param oValue
		 * @param nValue
		 * @param tlf
		 * 
		 */		
		private function changeEff(oValue :int,nValue :int,tlf :TextField):void
		{
			if(oValue == 0)
			{
				tlf.text = "" + nValue;
				return;
			}
			var obj :Object = {value:oValue};
			TweenLite.to(obj,.5,{value:nValue,onUpdate:updateH,onUpdateParams:[tlf,obj]});
		}
		private function updateH(tlf :TextField,param:Object):void
		{
			tlf.text = "" + int(param.value);
		}
		/**
		 *显示钱币效果 
		 * @param lastValue
		 * @param curValue
		 * @param coinType
		 * @param posMc
		 */		
		private function showCoinEffects(lastValue:int,
										 curValue:int,
										 coinType:int,
										 posMc :DisplayObject):void
		{
			if(lastValue == 0)
			{
				return;
			}
			var coinEffect 	:MovieClip;
			var tempMc :MovieClip;
			if(curValue > lastValue)//add
			{
				coinEffect = Reflection.createInstance("com.lz.res.CoinEffect");
				symbolSkin.addChild(coinEffect);
				coinEffect.x = posMc.x - 5;
				coinEffect.y = posMc.y - 3;
				coinEffect.gotoAndStop(1);
				tempMc = coinEffect.mc;
				tempMc.addEventListener(Event.ENTER_FRAME,onEnterFrame);
				tempMc.gotoAndPlay(1);
				function onEnterFrame(e :Event):void
				{
					var obj :DisplayObject;
					if(tempMc.mc)
					{
						for(var i :int = 0;i<tempMc.numChildren;i++)
						{
							obj = tempMc.getChildAt(i);
							if(obj && obj.name == "mc")
							{
								(obj as MovieClip).gotoAndStop(coinType);
							}
							i ++;
						}
					}
					if(tempMc.currentFrame == tempMc.totalFrames)
					{
						tempMc.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
						if(coinEffect && coinEffect.parent)
						{
							tempMc.gotoAndStop(1);
							coinEffect.gotoAndStop(1);
							coinEffect.parent.removeChild(coinEffect);
						}
					}
				}
			}
			/*else if(curValue < lastValue)//reduce
			{
				coinEffect = Reflection.createInstance("com.lz.res.CoinEffect");
				symbolSkin.addChild(coinEffect);
				coinEffect.x = posMc.x;
				coinEffect.y = posMc.y;
				coinEffect.gotoAndStop(2);
				tempMc = coinEffect.mc;
				tempMc.mc.gotoAndStop(coinType);
				tempMc.addFrameScript(tempMc.totalFrames - 1,onCompletePlay);
				function onCompletePlay():void
				{
					if(coinEffect && coinEffect.parent)
					{
						tempMc.gotoAndStop(1);
						coinEffect.gotoAndStop(1);
						coinEffect.parent.removeChild(coinEffect);
					}
				}
			}*/
		}
		/**
		 * 
		 * 添加事件
		 */		
		public function addEvtListener():void
		{
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.BASE_INFO_CHANGE,onDataChangeHandler);
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.TEAMICON_CHANGE,onTeamIconChangeHandler);
			expMC.addEventListener(MouseEvent.ROLL_OVER,onExpOverHandler);
			expMC.addEventListener(MouseEvent.ROLL_OUT,onExpOutHandler);
			euroAddBtn.addEventListener(MouseEvent.CLICK,onAddHandler);
			cashAddBtn.addEventListener(MouseEvent.CLICK,onAddHandler);
		}
		/**
		 *增加 handler 
		 * @param e
		 * 
		 */		
		private function onAddHandler(e :MouseEvent):void
		{
			switch(e.target)
			{
				case euroAddBtn://欧元购买
				{
					AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.Open_Shop_View));
					break;
				}
				case cashAddBtn://点券充值
				{
					JsClient.recharge(2);
					break;
				}
			}
		}
		/**
		 * exp roll over
		 * show value
		 * @param e
		 * 
		 */		
		private function onExpOverHandler(e :MouseEvent):void
		{
			var clubInfo :ClubInformation = AppData.getInstance().clubDatamanager.clubInfo;
			var info :String = "经验" + "" + clubInfo.exp + "/" + clubInfo.nextexp + "";
			TipsManager.show(TipsConst.BASETIPS,{tip:info},10,10);
		}
		/**
		 * exp roll out
		 * hide
		 * @param e
		 * 
		 */		
		private function onExpOutHandler(e :MouseEvent):void
		{
			TipsManager.hide();
		}
		/**
		 *数据变化 
		 * @param e
		 * 
		 */		
		private function onDataChangeHandler(e :Event):void
		{
			setData();
		}
		/**
		 *队徽变化 
		 * @param e
		 * 
		 */		
		private function onTeamIconChangeHandler(e :NotifierEvent):void
		{
			if(AppData.getInstance().clubDatamanager.clubInfo == null)return;
			setTeamIcon(AppData.getInstance().clubDatamanager.clubInfo.logo);
		}
		/**
		 * 
		 * 删除事件
		 */		
		public function removeEvtListener():void
		{
			
		}
		/**
		 * 销毁
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			symbolSkin = null;
		}
	}
}